<?php
 import("@.ORG.Page");
 import('@.ORG.Util.HashMap');
class DiscussAction extends Action{
    //成长曲线数据库模型曲线对象
    protected $Dis;
    //添加界面《调试用》
    public function Index()
    {
       
      $this->display("Tpl/default/Discuss/page2.html"); 
    }
    //获取添加数据
    public function category()
    {
        $cattag=$_REQUEST['catid'];
        $Dis = M('discuss');
        $condition['tag']=$cattag;      
        $count=$Dis->where($condition)->count();  
        $p = new Page ($count,5);
          //获得blog的列表
        $List =$Dis->where($condition)->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
        $List1=$Dis->where($condition)->limit($p->firstRow.','.$p->listRows)->order('count desc')->findAll();       
        $p->setConfig('header','条记录');
        $p->setConfig('prev',"<");
        $p->setConfig('next','>');
        $p->setConfig('first','<<');
        $p->setConfig('last','>>');               
        $page = $p->show();
   
        foreach($List AS $key=>$val){
          $List[$key]['wtime']=date('Y-m-d H:i',$val['wtime']); 
        }
        foreach($List1 AS $key1=>$val1){     
          $List[$key1]['wtime']=date('Y-m-d H:i',$val1['wtime']); 
        }
        $this->assign("page",$page);
        $this->assign("list",$List);
        $this->assign("list1",$List1);
        $this->assign("picid",$cattag);
        $this->display("Tpl/default/Discuss/page3.html");
         
    }
    
    function toChild(){
        $uid = $_REQUEST['uid'];
        $Child = A('Child');
        $Child->getChild($uid);
    }
   /**
    * @author sudi
    * 查看文章
    */
   public function detail()
   {
       $detailId = $_REQUEST['deid']; 
      // $catid = $_REQUEST['catid']; 
       $Dis = D('discuss');
       $condition['id']=$detailId;
       //查找该文章
       $List = $Dis->where($condition)->find();
       $List['wtime']=date('Y-m-d H:i',$List['wtime']); 
       //查找用户信息
       $condparent['nick'] = $List['p_name'];
       $Parent = M('Parent')->where($condparent)->find();
       $conduser['pid'] = $Parent['p_id'];
       $User = M('User')->where($conduser)->findAll();
       //查找该文章评论
       $cond['dis_id'] = $detailId;       
       $count=D('dis_comment')->where($cond)->count();
       $p = new Page ($count,3);
       $CommentList=D('dis_comment')->where($cond)->limit($p->firstRow.','.$p->listRows)->findAll();
       $p->setConfig('header','条记录');
       $p->setConfig('prev',"<");
       $p->setConfig('next','>');
       $p->setConfig('first','<<');
       $p->setConfig('last','>>');
       $page = $p->show();
       
       foreach($CommentList AS $key=>$val){
          $CommentList[$key]['ptime']=date('Y-m-d H:i',$val['ptime']); 
        }
       $this->assign("CommentList",$CommentList);
       $this->assign('page',$page);
       $this->assign("list",$List);
       $this->assign("parent",$Parent);
       $this->assign("user",$User);
      // $this->assign("picid",$catid);
       $this->display("Tpl/default/Discuss/onedisscuss.html");
   }
   /**
    * @author sudi
    *  写讨论题目
    */
    
    public function writeArticle()
    {
        $this->display("Tpl/default/Discuss/maincontent.html");
    }
    
   public function addArticle()
   {
       $title = $_POST['title'];
       $contents = $_POST['contents'];
       $tag = $_POST['selvalue'];
       $Dis = M('discuss');
       $data['content']= $contents;
    
       //需要添加Session
       $data['p_name'] = Session::get('nick');
       
       $data['title'] = $title;
       $data['tag'] = $tag;
       $data['wtime'] = time();
       $last = $Dis->add($data);
       $this->ajaxReturn($last,$tag,1,"JSON");
    }
    /**
     * 保存修改记录
     */
      public function saveArticle()
   {
       $title = $_POST['title'];
       $disid = $_POST['disid'];
       $contents = $_POST['contents'];
       $tag = $_POST['selvalue'];
       $Dis = M('discuss');
       $data['content']= $contents; 
       //需要添加Session
       $data['p_name'] = Session::get('nick');   
       $data['title'] = $title;
       $data['tag'] = $tag;
       $data['wtime'] = time();
       $condtion['id'] = $disid;
       $last = $Dis->where($condtion)->save($data);
       $this->ajaxReturn($last,$tag,1,"JSON");
    }
    /**
     * @author sudi
     * @time 2012年3月6日
     * 展示我所写该类别文章myArticle
     */
     public function myArticle()
     {
         //需要添加Session
        $cattag=$_REQUEST['catid'];
        $Dis = M('discuss');
        $condition['tag']=$cattag;
        $List = $Dis->where($condition)->findAll();
        foreach($List AS $key=>$val){
          $List[$key]['wtime']=date('Y-m-d H:i',$val['wtime']); 
        }
        $this->assign("list",$List);
        $this->assign("picid",$cattag);
        $this->display("Tpl/default/Discuss/page3.html");  
    }
    /**
     * @author sudi
     * @time 2012年3月6日
     * 展示我所写全部文章myArticle
     */
     function toOneArticle(){
        $pid = $_REQUEST['pid'];
        $condtion['p_id'] = $pid;
        $Parent = M('Parent')->where($condtion)->find();
        $this->AllArticle($Parent['nick']);
     }
    
     /**
     * @author sudi
     * @time 2012年3月6日
     * 展示我所写全部文章myArticle
     */
     public function myAllArticle(){
        $nick=Session::get('nick');
        $this->AllArticle($nick);
     }
     public function AllArticle($nick)
     {
     //需要添加Session
        if(empty($nick)){
             echo"<script>alert('你没有登录请登录！');
             window.history.back();</script>";
             exit;
        }
        $Dis = M('discuss');
        $condition['p_name']=$nick; 
        $count=$Dis->where($condition)->count();
        $p = new Page ($count,8);
        $List=$Dis->where($condition)->limit($p->firstRow.','.$p->listRows)->findAll();
        $p->setConfig('header','条记录');
        $p->setConfig('prev',"<");
        $p->setConfig('next','>');
        $p->setConfig('first','<<');
        $p->setConfig('last','>>');
        $page = $p->show();
        foreach($List AS $key=>$val){
          $List[$key]['wtime']=date('Y-m-d H:i',$val['wtime']); 
          $List[$key]['content'] =$this->cut_str($val['content'],50,0,'UTF-8');
        }
        $this->assign("list",$List);
        $this->assign("picid",$cattag);
        $this->assign("page",$page);
        $this->display("Tpl/default/Discuss/discusslist.html");  
    }
    /**
     * @author sudi
     * @time 2012年3月7日
     *添加评论
     */
    public function addcomment(){
         $comments = $_POST['comm'];
         $atrId = $_POST['aid'];
         if($comments == null||$atrId==""){
            $this->ajaxReturn("","没有添加成功",0,'json');  
         }
         $Comm = M('Dis_comment');
         $data['content']= $comments;
         $data['p_name'] = Session::get('nick');
         $data['dis_id']= $atrId;
         $data['ptime']= time();     
         $last = $Comm->add($data);
         if($last>0){
            $condtiondiscuss['id'] = $atrId;
            $count = M('Discuss')->where($condtiondiscuss)->find();
            $newcount = $count['count']+1;
            $datadis['count'] = $newcount;
            $lastdis=  M('Discuss')->where($condtiondiscuss)->save($datadis);
         }  
         $condtion['id'] = $last;
         $List= $Comm->where($condtion)->find();
         $content = $List['content']; 
         $pTime = date("Y-m-d H:i",$List['ptime']);
         $this->ajaxReturn($newcount,$pTime,1,'json');  
    
    }
    /**
     * @author sudi
     * @time 2012年3月7日
     * 编辑话题
     */
    public function editDiscuss(){
         $disid = $_REQUEST['disid']; 
         $condtion['id'] = $disid;
         $List= D('discuss')->where($condtion)->find();
         $this->assign('discuss',$List);
         $this->display("Tpl/default/Discuss/editdiscuss.html");
    
    }
     /**
     * @author sudi
     * @time 2012年3月7日
     * 删除话题deleteDiscuss
     */
    public function deleteDiscuss(){
         $disid = $_REQUEST['disid'];
         $cond['dis_id'] =  $disid;
         $delid = M('Dis_comment')->where($cond)->delete(); 
         if(empty($delid)){
            $this->error('删除出错，请重新删除'); 
         }
         $Comm = M('discuss');
         $condtion['id'] = $disid;
         $Last= $Comm->where($condtion)->delete();   
         if(empty($Last)){
            $this->error('删除出错，请重新删除'); 
         }  
         $this->myAllArticle();
    }
    /**
     * 对评论文章的进行排序
     * hashmap
     * @author sudi
     */
     function sortDiscuss($hm){
      
      
     }
     /**
      * 查看回复信息
      */
    function lookforComment()
    {
       $nick = Session::get("nick");
       $condtion['p_name'] = Session::get("nick");
       $Discuss=D('discuss')->where($condtion)->findAll();
       //$cond['tag'] = 0;  
       $List = null;
       foreach($Discuss AS $key=>$val){
           $cond[$key]['dis_id'] = $val['id'];
           $cond[$key]['tag'] = 0;
           $CommentList[$key]=D('dis_comment')->where($cond[$key])->findAll();        
            if($CommentList[$key]!=null&&$CommentList[$key]['p_name'] != $nick ){
                 foreach($CommentList[$key] AS $key1=>$val1){
                    $CommentList[$key][$key1]['title'] = $val['title'];
                 }
              }          
       }     
        $i = 0;
       foreach($CommentList AS $key=>$val){
        //dump($val);
        foreach($val AS $key2=>$val2){
            $List[$i] = $val2;
            $i++;
         }
       } 
       $j=0;
       foreach($List AS $key3=>$val3){
          if($val3['p_name']!=$nick){
              $CommList[$j] = $val3;
              $j++;
          }          
        
       }
       $this->assign("list",$CommList);
       $this->display("Tpl/default/Discuss/lookfordis.html");
    }
     /**
    * @author sudi
    * 查看文章
    */
   public function lookdetail()
   {
       $detailId = $_REQUEST['deid']; 
       $comid = $_REQUEST['comid'];
       $condComm['id'] = $comid;
       
       $data['tag'] = 1;
       $CommentList=D('dis_comment')->where($condComm)->save($data);
      // $catid = $_REQUEST['catid']; 
       $Dis = D('discuss');
       $condition['id']=$detailId;
       //查找该文章
       $List = $Dis->where($condition)->find();
       $List['wtime']=date('Y-m-d H:i',$List['wtime']); 
       //查找用户信息
       $condparent['nick'] = $List['p_name'];
       $Parent = M('Parent')->where($condparent)->find();
       $conduser['pid'] = $Parent['p_id'];
       $User = M('User')->where($conduser)->findAll();
       //查找该文章评论
       $cond['dis_id'] = $detailId;       
       $count=D('dis_comment')->where($cond)->count();
       $p = new Page ($count,3);
       $CommentList=D('dis_comment')->where($cond)->limit($p->firstRow.','.$p->listRows)->findAll();
       $p->setConfig('header','条记录');
       $p->setConfig('prev',"<");
       $p->setConfig('next','>');
       $p->setConfig('first','<<');
       $p->setConfig('last','>>');
       $page = $p->show();
       
       foreach($CommentList AS $key=>$val){
          $CommentList[$key]['ptime']=date('Y-m-d H:i',$val['ptime']); 
        }
       $this->assign("CommentList",$CommentList);
       $this->assign('page',$page);
       $this->assign("list",$List);
       $this->assign("parent",$Parent);
       $this->assign("user",$User);
      // $this->assign("picid",$catid);
       $this->display("Tpl/default/Discuss/onedisscuss.html");
   }
    
    function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') 
      { 
            if($code == 'UTF-8') 
            { 
            $pa ="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; 
            preg_match_all($pa, $string, $t_string); if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)); 
            return join('', array_slice($t_string[0], $start, $sublen)); 
            } 
            else 
            { 
            $start = $start*2; 
            $sublen = $sublen*2; 
            $strlen = strlen($string); 
            $tmpstr = ''; for($i=0; $i<$strlen; $i++) 
            { 
            if($i>=$start && $i<($start+$sublen)) 
            { 
            if(ord(substr($string, $i, 1))>129) 
            { 
            $tmpstr.= substr($string, $i, 2); 
            } 
            else 
            { 
            $tmpstr.= substr($string, $i, 1); 
            } 
            } 
            if(ord(substr($string, $i, 1))>129) $i++; 
            } 
            if(strlen($tmpstr)<$strlen ) $tmpstr.= ""; 
            return $tmpstr; 
            } 
        } 

    
}
?>